package cn.prophet.service.impl;

import cn.prophet.bean.vo.AliBankBean;
import cn.prophet.config.UrlConfig;
import cn.prophet.service.ForeignService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;

import org.springframework.web.client.RestTemplate;


import javax.annotation.Resource;

import java.util.HashMap;
import java.util.Map;
@Service
@Slf4j
public class ForeignServiceImpl implements ForeignService{
    @Resource
    private RestTemplate restTemplate;
    @Override
    public AliBankBean getBankName(String bankNum) {
        Map<String,String> map = new HashMap<>();
        map.put("cardNo",bankNum);
        map.put("cardBinCheck","true");
        AliBankBean aliBankBean = restTemplate.getForObject(UrlConfig.aliBankUrl,AliBankBean.class,map);
        if(aliBankBean!=null){
            String validate = aliBankBean.getValidated();
            if("true".equals(validate)){
                //是银行卡号
                if("DC".equals(aliBankBean.getCardType())){
                    //是储蓄卡
                    String bank = aliBankBean.getBank();
                    JSONObject jsonObject = JSON.parseObject(UrlConfig.bankNameType);
                    String value = jsonObject.getString(bank);
                    aliBankBean.setBankName(value);

                }
            }
        }
        return aliBankBean;
    }

    @Override
    public Map<String,String> getNxOpenCode() {
        try {
            Document document = Jsoup.connect(UrlConfig.nxOrionUrl).userAgent(
                    "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36")
                    .referrer(UrlConfig.nxOrionUrl).get();
            Elements els = document.select("body > div > section > a > div.list-group-item");
            Element els1 = els.get(0);
            Element h5El = els1.getElementsByTag("h5").get(0);
            Elements openCodeEl = els1.getElementsByTag("p").get(0).getElementsByTag("span");
            String num = h5El.getElementsByTag("span").get(0).text();
            StringBuffer sb = new StringBuffer();
            Map<String,String> map = new HashMap<>(2);
            for (int i = 0;i<openCodeEl.size();i++){
                String value = openCodeEl.get(i).text();
                sb.append(value);
                if (i != (openCodeEl.size() - 1)) {
                    sb.append(",");
                }

            }
            map.put("num",num.substring(1,num.length()-1));
            map.put("openCode",sb.toString());

            return map;
        }catch (Exception e){
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public Map<String, String> getJsOpenCode() {
        Map<String,String> map = new HashMap<>();
        try {
            Document document = Jsoup.connect(UrlConfig.jsGovUrl).userAgent(
                    "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36")
                    .referrer(UrlConfig.jsGovUrl).get();
            Elements els = document.getElementById("firstTbody").getElementsByTag("tr");
            Element lastEl =null;
            for (int i = 0;i<els.size();i++){
                String classAttr = els.get(i).attr("class");

                if("hidetr".equals(classAttr)){
                    break;
                }
                lastEl = els.get(i);
            }
            //190115-063
            Elements elss = lastEl.getElementsByTag("td");
            String numStr = elss.get(0).text();
            String [] mArray = numStr.split("-0");
            StringBuffer openCode = new StringBuffer();
            Elements ems = lastEl.getElementsByClass("lottery_box").first().getElementsByTag("em");

            map.put("num","20"+mArray[0]+mArray[1]);
            map.put("openCode",ems.get(0).text()+","+ems.get(1).text()+","+ems.get(2).text());
            return map;


        }catch (Exception e){
            e.printStackTrace();
        }

        return null;
    }
}
